package com.wantong.jianpu.dialogs;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.core.BottomPopupView;
import com.wantong.jianpu.R;
import com.wantong.jianpu.adapters.LocationContentAdapter;
import com.wantong.jianpu.adapters.LocationTitleAdapter;
import com.wantong.jianpu.beans.LocationBean;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;


/**
 * @author PACY
 * @date 2022/5/23
 * description:
 */
public class LocationPopupView extends BottomPopupView {
    private LinearLayoutManager titleManager;
    private LocationTitleAdapter titleAdapter;
    private RecyclerView titleRv;

    private LinearLayoutManager contentManager;
    private LocationContentAdapter contentAdapter;
    private RecyclerView contentRv;


    private List<LocationBean> locationBeanList;
    private List<LocationBean> titleList=new ArrayList<>();

    private selectLocationCallBack callBack;

    private TextView commit_tv;

    public LocationPopupView(@NonNull Context context,List<LocationBean> locationBeanList) {
        super(context);
        this.locationBeanList = locationBeanList;
    }

    public void setCallBack(selectLocationCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.location_popup_view;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initAdapter();
    }

    private void initAdapter(){
        titleManager=new LinearLayoutManager(getContext(),RecyclerView.HORIZONTAL,false);
        titleAdapter=new LocationTitleAdapter();
        titleRv=findViewById(R.id.title);
        titleRv.setLayoutManager(titleManager);
        titleRv.setAdapter(titleAdapter);
        titleAdapter.setNewInstance(titleList);

        titleAdapter.setOnItemClickListener((adapter, view, position) -> {
            int size = titleAdapter.getItemCount() - 1;
            for (int i = size; i > position; i--) {
                titleAdapter.removeAt(i);
            }
            titleAdapter.notifyDataSetChanged();
            contentAdapter.setNewInstance(titleAdapter.getItem(position).getChildren());
        });

        contentManager=new LinearLayoutManager(getContext());
        contentAdapter=new LocationContentAdapter();
        contentRv=findViewById(R.id.content);
        contentRv.setLayoutManager(contentManager);
        contentRv.setAdapter(contentAdapter);
        contentAdapter.setNewInstance(locationBeanList);


        contentAdapter.setOnItemClickListener((adapter, view, position) -> {
            if(contentAdapter.getItem(position).getChildren().isEmpty()){
                callBack.location(contentAdapter.getItem(position));
                dismiss();
            }else{
                titleAdapter.addData(contentAdapter.getItem(position));
                titleAdapter.notifyDataSetChanged();
                contentAdapter.setNewInstance(contentAdapter.getItem(position).getChildren());
            }
        });

        commit_tv=findViewById(R.id.commit_tv);
        commit_tv.setOnClickListener(v -> {
            if(titleAdapter.getItemCount()<1){
                ToastUtils.show("请先选择主要位置");
            }else{
                callBack.location(titleAdapter.getItem(titleAdapter.getItemCount()-1));
                dismiss();
            }

        });
    }


    public interface selectLocationCallBack{
        void location(LocationBean location);
    }

}
